package top.tupobi.zing.utils;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.MediaStore;

import top.tupobi.zing.ZingApplication;
import top.tupobi.zing.broadcast.NotificationClickReceiver;

public class NotificationUtils {

    //开始通知
    public static void startNotificationManager(String title, int iconId, String content) {
        Context applicationContext = ZingApplication.getAppContext();

        NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(applicationContext, NotificationClickReceiver.class);

//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, intent, 0);

        long[] vibrate = {0, 500, 1000, 1500};

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder notification = new Notification.Builder(applicationContext)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setWhen(System.currentTimeMillis())// O以上用下面的
                    .setShowWhen(true)
                    .setSmallIcon(iconId)
                    .setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), iconId))
                    .setVibrate(vibrate)
                    .setContentIntent(pendingIntent)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setChannelId(applicationContext.getPackageName())
                    .setAutoCancel(true)// 点击后状态栏自动消失
                    .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);

            NotificationChannel channel = new NotificationChannel(
                    applicationContext.getPackageName(),
                    "通知消息",
                    NotificationManager.IMPORTANCE_HIGH
            );

            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(0, notification.build());

        } else {

            Notification.Builder notification = new Notification
                    .Builder(applicationContext)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(iconId)
                    .setLargeIcon(BitmapFactory.decodeResource(applicationContext.getResources(), iconId))
                    .setVibrate(vibrate)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setWhen(System.currentTimeMillis())
                    .setPriority(Notification.PRIORITY_MAX)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
            notificationManager.notify(0, notification.build());
        }
    }


}
